package com.tgra;

import com.badlogic.gdx.Gdx;

public class MovingCube
{
	Cube cube;
	
	float startTime, meanTime1, meanTime2, meanTime3, endTime;
	Point3D P1, P2, P3, P4, P5, P6, P7, P8, P9, P10, P11, P12, P13;
	Point3D position;
	
	float rotationAngle;

	public MovingCube()
	{
		cube = new Cube("assets/crate.jpg");

		startTime = 2.0f;
		endTime = 20.0f;
		meanTime1 = 5.0f;
		meanTime2 = 10.0f;
		meanTime3 = 15.0f;
		
		
		P1 = new Point3D(0.0f, 0.0f, 0.0f);
		P2 = new Point3D(0.5f, 0.0f, 7.5f);
		P3 = new Point3D(1.0f, 0.0f, 9.0f);
		P4 = new Point3D(1.2f, 0.0f, 6.0f);
		
		P5 = new Point3D(4.0f, 0.0f, -2.0f);
		P6 = new Point3D(8.5f, 0.0f, -2.0f);
		P7 = new Point3D(9.0f, 0.0f, 4.5f);
		
		P8 = new Point3D(8.5f, 0.0f, 9.0f);
		P9 = new Point3D(6.5f, 0.0f, 9.0f);
		P10 = new Point3D(5.5f, 0.0f, 7.5f);
		
		P11 = new Point3D(3.3f, 0.0f, 6.0f);
		P12 = new Point3D(2.9f, 0.0f, 4.0f);
		P13 = new Point3D(4.5f, 0.0f, 4.0f);
		
		position = new Point3D(0.0f, 0.0f, 0.0f);
		
		rotationAngle = 0.0f;
	}
	
	public void update(float elapsedTime)
	{
		if(elapsedTime < startTime)
		{
			position.x = P1.x;
			position.y = P1.y;
			position.z = P1.z;
			
		}
		else if (elapsedTime > endTime)
		{
			position.x = P13.x;
			position.y = P13.y;
			position.z = P13.z;
			
			
		}
		else if (elapsedTime > startTime && elapsedTime < meanTime1)
		{
			float t = (elapsedTime - startTime) / (meanTime1 - startTime);
			
			// Calculating position with Bezier.
			position.x = (1.0f - t)*(1.0f - t)*(1.0f - t)*P1.x
				    + 3*(1.0f - t)*(1.0f - t)*t * P2.x
				    + 3*(1.0f - t)*t*t * P3.x
				    + t*t*t * P4.x;
			
			position.z = (1.0f - t)*(1.0f - t)*(1.0f - t)*P1.z
				    + 3*(1.0f - t)*(1.0f - t)*t * P2.z
				    + 3*(1.0f - t)*t*t * P3.z
				    + t*t*t * P4.z;
			
		}
		else if (elapsedTime > meanTime1 && elapsedTime < meanTime2)
		{
			float t = (elapsedTime - meanTime1) / (meanTime2 - meanTime1);
			
			// Calculating position with Bezier.
			position.x = (1.0f - t)*(1.0f - t)*(1.0f - t)*P4.x
				    + 3*(1.0f - t)*(1.0f - t)*t * P5.x
				    + 3*(1.0f - t)*t*t * P6.x
				    + t*t*t * P7.x;
			
			position.z = (1.0f - t)*(1.0f - t)*(1.0f - t)*P4.z
				    + 3*(1.0f - t)*(1.0f - t)*t * P5.z
				    + 3*(1.0f - t)*t*t * P6.z
				    + t*t*t * P7.z;
			
		}
		else if (elapsedTime > meanTime2 && elapsedTime < meanTime3 )
		{
			float t = (elapsedTime - meanTime2) / (meanTime3 - meanTime2);
			
			// Calculating position with Bezier.
			position.x = (1.0f - t)*(1.0f - t)*(1.0f - t)*P7.x
				    + 3*(1.0f - t)*(1.0f - t)*t * P8.x
				    + 3*(1.0f - t)*t*t * P9.x
				    + t*t*t * P10.x;
			
			position.z = (1.0f - t)*(1.0f - t)*(1.0f - t)*P7.z
				    + 3*(1.0f - t)*(1.0f - t)*t * P8.z
				    + 3*(1.0f - t)*t*t * P9.z
				    + t*t*t * P10.z;
			
		}
		else if (elapsedTime > meanTime3 && elapsedTime < endTime )
		{
			float t = (elapsedTime - meanTime3) / (endTime - meanTime3);
			
			// Calculating position with Bezier.
			position.x = (1.0f - t)*(1.0f - t)*(1.0f - t)*P10.x
				    + 3*(1.0f - t)*(1.0f - t)*t * P11.x
				    + 3*(1.0f - t)*t*t * P12.x
				    + t*t*t * P13.x;
			
			position.z = (1.0f - t)*(1.0f - t)*(1.0f - t)*P10.z
				    + 3*(1.0f - t)*(1.0f - t)*t * P11.z
				    + 3*(1.0f - t)*t*t * P12.z
				    + t*t*t * P13.z;
		
			
		}
		

		rotationAngle = elapsedTime * -90.0f;
	}
	
	public void display()
	{
		Gdx.gl11.glPushMatrix();
		Gdx.gl11.glTranslatef(position.x, 2.0f, position.z);
		Gdx.gl11.glScalef(0.5f, 0.5f, 0.5f);
		cube.draw();
		Gdx.gl11.glPopMatrix();
	}
}
